if (typeof ph == 'undefined')
  ph = {};

ph.__global__ = this;
ph.argv = arguments;

ph.bootstrap = function(settings) {
  if (settings)
    load(settings);
  
  if (typeof djConfig == 'undefined')
    djConfig = {};
  if (!djConfig.baseUrl)
    djConfig.baseUrl = DOJO_PATH + 'dojo/';
  if (!djConfig.libraryScriptUri)
    djConfig.libraryScriptUri = DOJO_PATH + 'dojo/';
  
  load(DOJO_PATH + 'dojo/dojo.js');
  
  for (var module in phModules) {
    if (phModules.hasOwnProperty(module))
      dojo.registerModulePath(module, phModules[module]);
  }
};

ph.keepRunning = function() {
  spawn(function() { 
    while(true) java.lang.Thread.sleep(1000000);
  });
};

ph.str = function(str) {
  return str + '';
};

ph._loadedModules = {};

ph.require = function(moduleName) {
  moduleName = /^Packages\./.test(moduleName) ? moduleName : 'Packages.' + moduleName;
  if (moduleName in ph._loadedModules)
    return ph._loadedModules[moduleName];
  
  var module = dojo.getObject(moduleName, false, ph.__global__);
  if (/\[JavaPackage\s/.test(module))
    throw new Error('Could not load module ' + moduleName.replace(/^Packages\./, ''));
  
  return ph._loadedModules = module;
};

encodeURIComponent = (function() {
  var encode = encodeURIComponent;
  return encode('=') == '%3D' ? encode : function() {
    return encode.apply(null, arguments).replace(/\%(..)/g, function(s) { return s.toUpperCase() });
  }
})();
